package com.rules.admin.service.decision.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.google.common.collect.Lists;
import com.rules.admin.dao.DecisionRowDao;
import com.rules.admin.entity.DecisionCell;
import com.rules.admin.entity.DecisionRow;
import com.rules.admin.service.decision.DecisionCellService;
import com.rules.admin.service.decision.DecisionRowService;
import com.rules.admin.vo.DecisionRowVO;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Service
public class DecisionRowServiceImpl extends ServiceImpl<DecisionRowDao, DecisionRow> implements DecisionRowService {

   @Autowired
   private DecisionCellService decisionCellService;


   public List<DecisionRow> getRowsByTableId(long tableId) {
//      DecisionRowExample example = new DecisionRowExample();
//      Criteria criteria = example.createCriteria();
//      criteria.andTableIdEqualTo(tableId);
//      example.setOrderByClause("priority desc, create_time asc");
//      return this.selectByExample(example);

      LambdaQueryWrapper<DecisionRow> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(DecisionRow::getTableId,tableId);
      queryWrapper.orderByDesc(DecisionRow::getPriority);
      queryWrapper.orderByAsc(DecisionRow::getCreateTime);
      return this.baseMapper.selectList(queryWrapper);
   }

   public void deleteRow(long rowId) {
//      DecisionCellExample example = new DecisionCellExample();
//      example.createCriteria().andRowIdEqualTo(rowId);
//      this.decisionCellService.deleteByExample(example);
      this.decisionCellService.getCellsByRowId(rowId);
      this.deleteRow(rowId);
   }

   public void addRow(DecisionRowVO row) {
      DecisionRow decisionRow = new DecisionRow();

      try {
         BeanUtils.copyProperties(decisionRow, row);
      } catch (IllegalAccessException var8) {
      } catch (InvocationTargetException var9) {
      }

      this.baseMapper.insert(decisionRow);
      DecisionCell[] cells = row.getCells();
      if (cells != null && cells.length > 0) {
         this.decisionCellService.saveBatch(Lists.newArrayList(cells));
      }

   }

   public List<DecisionRow> getEnableRows(long tableId) {
//      DecisionRowExample example = new DecisionRowExample();
//      Criteria criteria = example.createCriteria();
//      criteria.andTableIdEqualTo(tableId).andStatusEqualTo(true);
//      example.setOrderByClause("priority desc, create_time asc");

      LambdaQueryWrapper<DecisionRow> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(DecisionRow::getTableId,tableId);
      queryWrapper.orderByDesc(DecisionRow::getPriority);
      queryWrapper.orderByAsc(DecisionRow::getCreateTime);
      return this.baseMapper.selectList(queryWrapper);
   }
}
